﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    abstract class Figures
    {
        public Node Node1 { get; set; }
        public Node Node2 { get; set; }
        public Node Node3 { get; set; }
        public Node Node4 { get; set; }
        public ConsoleColor figureColor { get; set; }
        public FigurePosition currentPosition = FigurePosition.North;
        public FigurePosition nextPosition;

        public void MoveFigureDown()
        {
            Node1.MoveDown();
            Node2.MoveDown();
            Node3.MoveDown();
            Node4.MoveDown();
        }

        public void MoveFigureLeft()
        {
            Node1.MoveLeft();
            Node2.MoveLeft();
            Node3.MoveLeft();
            Node4.MoveLeft();
        }

        public void MoveFigureRight()
        {
            Node1.MoveRight();
            Node2.MoveRight();
            Node3.MoveRight();
            Node4.MoveRight();
        }

        public void CheckNextPosition(FigurePosition curPosit)
        {
            if (currentPosition == FigurePosition.North)
            {
                nextPosition = FigurePosition.East;
            }
            else if (currentPosition == FigurePosition.East)
            {
                nextPosition = FigurePosition.South;
            }
            else if (currentPosition == FigurePosition.South)
            {
                nextPosition = FigurePosition.West;
            }
            else if (currentPosition == FigurePosition.West)
            {
                nextPosition = FigurePosition.North;
            }
        }

        abstract public void FigureRotate();

    }

 
    class Cube : Figures
    {
        public Cube(GameField field)
        {
            figureColor = ConsoleColor.Magenta;
            Node1 = new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor);
            Node2 = new Node(Node1.XNode, Node1.YNode - 1, figureColor);
            Node3 = new Node(Node1.XNode + 1, Node1.YNode - 1, figureColor);
            Node4 = new Node(Node1.XNode + 1, Node1.YNode, figureColor);
            
        }

        public override void FigureRotate()
        {
            
        }
    }

    class Piptik : Figures
    {
        public Piptik(GameField field)
        {
            figureColor = ConsoleColor.Red;
            Node1 = new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor);
            Node2 = new Node(Node1.XNode - 1, Node1.YNode + 1, figureColor);
            Node3 = new Node(Node1.XNode, Node1.YNode + 1, figureColor);
            Node4 = new Node(Node1.XNode + 1, Node1.YNode + 1, figureColor);
        }

        public override void FigureRotate()
        {
            
            if (currentPosition == FigurePosition.North)
            {
                Node1.XNode += 1; Node1.YNode += 1;
                Node2.XNode += 1; Node2.YNode -= 1;
                Node4.XNode -= 1; Node4.YNode += 1;
                currentPosition = FigurePosition.East;
            }
            if (currentPosition == FigurePosition.East)
            {
                Node1.XNode -= 1; Node1.YNode += 1;
                Node2.XNode += 1; Node2.YNode += 1;
                Node4.XNode -= 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.South;
            }
            if (currentPosition == FigurePosition.South)
            {
                Node1.XNode -= 1; Node1.YNode -= 1;
                Node2.XNode -= 1; Node2.YNode += 1;
                Node4.XNode += 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.West;
            }
            if (currentPosition == FigurePosition.West)
            {
                Node1.XNode += 1; Node1.YNode -= 1;
                Node2.XNode -= 1; Node2.YNode -= 1;
                Node4.XNode += 1; Node4.YNode += 1;
                currentPosition = FigurePosition.North;
            }
        }     
    }

    class Horse : Figures
    {
        public Horse(GameField field)
        {
            figureColor = ConsoleColor.Green;
            Node1 = new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor);
            Node2 = new Node(Node1.XNode, Node1.YNode + 1, figureColor);
            Node3 = new Node(Node1.XNode + 1, Node1.YNode + 1, figureColor);
            Node4 = new Node(Node1.XNode + 2, Node1.YNode + 1, figureColor);
        }

        public override void FigureRotate()
        {
            CheckNextPosition(currentPosition);
            if (nextPosition == FigurePosition.East)
            {
                Node1.XNode += 2;
                Node2.XNode += 1; Node2.YNode -= 1;
                Node4.XNode -= 1; Node4.YNode += 1;
                currentPosition = FigurePosition.East;
            }
            if (nextPosition == FigurePosition.South)
            {
                Node1.YNode += 2;
                Node2.XNode += 1; Node2.YNode += 1;
                Node4.XNode -= 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.South;
            }
            if (nextPosition == FigurePosition.West)
            {
                Node1.XNode -= 2;
                Node2.XNode -= 1; Node2.YNode += 1;
                Node4.XNode += 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.West;
            }
            if (nextPosition == FigurePosition.North)
            {
                Node1.YNode -= 2;
                Node2.XNode -= 1; Node2.YNode -= 1;
                Node4.XNode += 1; Node4.YNode += 1;
                currentPosition = FigurePosition.North;
            }
        }
    }

    class Twos : Figures
    {
        public Twos(GameField field)
        {
            figureColor = ConsoleColor.Blue;
            Node1 = new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor);
            Node2 = new Node(Node1.XNode + 1, Node1.YNode, figureColor);
            Node3 = new Node(Node1.XNode + 1, Node1.YNode + 1, figureColor);
            Node4 = new Node(Node1.XNode + 2, Node1.YNode + 1, figureColor);
        }

        public override void FigureRotate()
        {
            CheckNextPosition(currentPosition);
            if (nextPosition == FigurePosition.East)
            {
                Node1.XNode += 2;
                Node2.XNode += 1; Node2.YNode += 1;
                Node4.XNode -= 1; Node4.YNode += 1;
                currentPosition = FigurePosition.West;
            }
            if (nextPosition == FigurePosition.North)
            {
                Node1.XNode -= 2;
                Node2.XNode -= 1; Node2.YNode -= 1;
                Node4.XNode += 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.North;
            }

        }
    }

    class Stick : Figures
    {
        public Stick(GameField field)
        {
            figureColor = ConsoleColor.White;
            Node1 = new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor);
            Node2 = new Node(Node1.XNode + 1, Node1.YNode, figureColor);
            Node3 = new Node(Node1.XNode + 2, Node1.YNode, figureColor);
            Node4 = new Node(Node1.XNode + 3, Node1.YNode, figureColor);
        }

        public override void FigureRotate()
        {
            CheckNextPosition(currentPosition);
            if (nextPosition == FigurePosition.East)
            {
                Node1.XNode += 2; Node1.YNode -= 2;
                Node2.XNode += 1; Node2.YNode -= 1;
                Node4.XNode -= 1; Node4.YNode += 1;
                currentPosition = FigurePosition.West;
            }
            if (nextPosition == FigurePosition.North)
            {
                Node1.XNode -= 2; Node1.YNode += 2;
                Node2.XNode -= 1; Node2.YNode += 1;
                Node4.XNode += 1; Node4.YNode -= 1;
                currentPosition = FigurePosition.North;
            }

        }

    }
}
